import argparse
from vulnerability.database import CVEDatabase
from vulnerability.tracker import CVETracker
from vulnerability.call import create_comment, make_body

def main():
    parser = argparse.ArgumentParser(description='查询指定 CVE 名称在不同 OpenEuler 版本中的信息。')
    parser.add_argument('cve_name', type=str, help='要查询的 CVE 名称，例如 CVE-2023-1234')
    parser.add_argument('access_token', type=str)
    parser.add_argument('full_name', type=str)
    parser.add_argument('number', type=str)
 
    args = parser.parse_args()
    cve_name = str(args.cve_name).strip()
    access_token = str(args.access_token).strip()
    full_name = str(args.full_name).strip()
    number = str(args.number).strip()

    print("读取数据库")
    cve_db = CVEDatabase('fixed_commit_link')
    tracker = CVETracker(cve_db)
    print("查询CVE")
    try:
        tracker.update_cve_commit(cve_name)
    except Exception as e:
        print(f"查询CVE失败：{str(e)}")
    print(f"查询每个OS是否引入{cve_name}")
    code_dict, intro_commit, fixed_commit = tracker.query_all_os_cve(cve_name)
    result_dict = {}
    for os_name, code in code_dict.items():
        print(f"检查{cve_name}在{os_name}中的引入及修复情况")
        result_dict[os_name] = tracker.get_analyze_result(code)

    body = make_body(result=result_dict, tracker=tracker)
    body += "\n"
    body += f"引入补丁链接：https://web.git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id={intro_commit}\n"
    body += f"修复补丁链接：https://web.git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id={fixed_commit}\n"

    cve_db.close()
    parameter = {
        "access_token": access_token,
        "full_name": full_name,
        "number": number,
    }
    create_comment(parameter, body)

if __name__ == "__main__":
    result = main()
